package com.wyg.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.wyg.dao.QuestionDao;
import com.wyg.dao.UserDao;
import com.wyg.dao.impl.QuestionDaoImpl;
import com.wyg.dao.impl.UserDaoImpl;
import com.wyg.pojo.Test;
import com.wyg.pojo.User;
import com.wyg.service.UserService;

import oracle.sql.DATE;

public class UserServiceImpl implements UserService {
	
	private HttpServletRequest request;
	private HttpServletResponse response;
	private UserDao udi;
	private QuestionDao qdi;

	public UserDao getUdi() {
		return udi;
	}

	public void setUdi(UserDao udi) {
		this.udi = udi;
	}

	public QuestionDao getQdi() {
		return qdi;
	}

	public void setQdi(QuestionDao qdi) {
		this.qdi = qdi;
	}

	@Override
	public String showPartQuestion() {
		List list = qdi.showPartQuestion();
		this.request = ServletActionContext.getRequest();
		request.setAttribute("questions", list);
		return "showPartQuestion";
	}

	@Override
	public String showChoiceQuestion() {
		List list = qdi.showChoiceQuestion();
		this.request = ServletActionContext.getRequest();
		request.setAttribute("questions", list);
		return "showChoiceQuestion";
	}

	@Override
	public String showPanduanQuestion() {
		List list = qdi.showPanduanQuestion();
		this.request = ServletActionContext.getRequest();
		request.setAttribute("questions", list);
		return "showPanduanQuestion";
	}

	@Override
	public String doActive(int id) {
		udi.doActive(id);
		User u = udi.getUser(id);
		this.request = ServletActionContext.getRequest();
		request.getSession().setAttribute("user", u);
		return "dook";
	}
	//考试报名
	@Override
	public String registTest(Test test) {
		Calendar c = Calendar.getInstance();
		int syear = c.get(Calendar.YEAR);
		int sMonth = c.get(Calendar.MONTH)+1;
		int sDay = c.get(Calendar.DATE);
		
		Date date1 = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String datestring = sdf.format(date1);
		
		if(datestring.compareTo(Test.startTime)<=0){
			String date = syear+"-"+sMonth+"-"+sDay;
			test.setTestTime(date);
			udi.registTest(test);
			return "signSuccess";
		}else{
			return "notAllowSign";
		}
	}

	@Override
	public String checkSign(String phone) {
		Calendar c = Calendar.getInstance();
		Date date1 = c.getTime();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		String datestring = sdf.format(date1);
		
		String startTime = Test.startTime;
		String endTime = Test.endTime;
		
		if(datestring.compareTo(startTime)>=0&&datestring.compareTo(endTime)<=0){
			Test t = udi.getTestByPhone(phone);
			if(t==null){
				return "noSign";
			}else{
				this.request = ServletActionContext.getRequest();
				request.getSession().setAttribute("test", t);
				return "checkInfo";
			}
		}else if(datestring.compareTo(startTime)<0){
			this.request = ServletActionContext.getRequest();
			request.getSession().setAttribute("startTime", Test.startTime); 
			return "tooearly";
		}else{
			return "toolate";
		}
		
		
		
		
	}

	@Override
	public String showTestQuestion() {
		List list = qdi.showPartQuestion();
		this.request = ServletActionContext.getRequest();
		request.setAttribute("questions", list);
		return "showTestQuestion";
	}

}
